package com.cq.smartelebicycle;

import com.cq.smartelebicycle.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity implements OnClickListener{
	
	private ImageView  mPswImg;
	private TextView titleTx;
	private TextView pswChangeTx;
	private TextView cancelTx;
	
	
	private static final String  FACTORY_PSW="1234";
	private static final int  FACTORY=100;
	private static final int  CHANGE_PSW=101;
	private static final int  INPUT_PSW=102;
	private int mCurrPswState = -100;
	private int psw_action = FACTORY;
	private String userPsw =null;

	
	private DataManger  dm;
	private String inputPsw="" ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		dm = new DataManger(this);
		
		userPsw = dm.getUserPsw();
		if(userPsw!=null){
			psw_action = INPUT_PSW;
		}
		
		mCurrPswState = psw_action;
		
		mPswImg =(ImageView)findViewById(R.id.pwd_icon);
		ImageView  mDigit1 =(ImageView)findViewById(R.id.digit_1);
		ImageView  mDigit2 =(ImageView)findViewById(R.id.digit_2);
		ImageView  mDigit3 =(ImageView)findViewById(R.id.digit_3);
		ImageView  mDigit4 =(ImageView)findViewById(R.id.digit_4);
		ImageView  mDigit5 =(ImageView)findViewById(R.id.digit_5);
		ImageView  mDigit6 =(ImageView)findViewById(R.id.digit_6);
		ImageView  mDigit7 =(ImageView)findViewById(R.id.digit_7);
		ImageView  mDigit8 =(ImageView)findViewById(R.id.digit_8);
		ImageView  mDigit9 =(ImageView)findViewById(R.id.digit_9);
		ImageView  mDigit0 =(ImageView)findViewById(R.id.digit_0);
		mDigit0.setOnClickListener(this);
		mDigit1.setOnClickListener(this);
		mDigit2.setOnClickListener(this);
		mDigit3.setOnClickListener(this);
		mDigit4.setOnClickListener(this);
		mDigit5.setOnClickListener(this);
		mDigit6.setOnClickListener(this);
		mDigit7.setOnClickListener(this);
		mDigit8.setOnClickListener(this);
		mDigit9.setOnClickListener(this);
		
		
		
		titleTx =(TextView)findViewById(R.id.title_id);
		pswChangeTx =(TextView)findViewById(R.id.change_psw);

		updateView();
		
		cancelTx =(TextView)findViewById(R.id.cancle_psw);
		cancelTx.setOnClickListener(this);
		
		
		
	}
	
	
	private void updateView(){
		if(psw_action==INPUT_PSW){
			titleTx.setText(R.string.input_psw_title);
			pswChangeTx.setVisibility(View.VISIBLE);
			pswChangeTx.setOnClickListener(this);
		}else{
			titleTx.setText(R.string.factory_psw_title);
			pswChangeTx.setVisibility(View.INVISIBLE);
			pswChangeTx.setOnClickListener(null);
		}
	}
	
	@Override
	public void onClick(View view) {
		// TODO Auto-generated method stub
		int id = view.getId();
		switch (id) {
		case R.id.digit_1:
			inputPsw +="1";
			break;
		case R.id.digit_2:
			inputPsw +="2";
			break;
		case R.id.digit_3:
			inputPsw +="3";
			break;
		case R.id.digit_4:
			inputPsw +="4";
			break;
		case R.id.digit_5:
			inputPsw +="5";
			break;
		case R.id.digit_6:
			inputPsw +="6";
			break;
		case R.id.digit_7:
			inputPsw +="7";
			break;
		case R.id.digit_8:
			inputPsw +="8";
			break;
		case R.id.digit_9:
			inputPsw +="9";
			break;
		case R.id.digit_0:
			inputPsw +="0";
			break;

		case R.id.change_psw:
			userPsw=null;
			inputPsw="";
			titleTx.setText(R.string.input_new_psw_title);
			mPswImg.setImageResource(R.drawable.pwd_icon0);
			mCurrPswState = psw_action;
			psw_action=CHANGE_PSW;
			break;
		case R.id.cancle_psw:
			psw_action=mCurrPswState;
			inputPsw="";
			mPswImg.setImageResource(R.drawable.pwd_icon0);
			updateView();
			
			break;
		default:
			break;
		}
		checkPsw();
		
		
	}


	private void checkPsw(){
		int  length = inputPsw.length();
		Log.d("SIMMON"," length= "+length);
		Log.d("SIMMON"," inputPsw= "+inputPsw);
		switch(length){
		case 1:
			mPswImg.setImageResource(R.drawable.pwd_icon1);
			break;
		case 2:
			mPswImg.setImageResource(R.drawable.pwd_icon2);
			break;
		case 3:
			mPswImg.setImageResource(R.drawable.pwd_icon3);
			break;
		case 4:
			mPswImg.setImageResource(R.drawable.pwd_icon4);
			if(psw_action==FACTORY){
			    ckeckFactoryPsw();
			}else if(psw_action==CHANGE_PSW){
				inputUserNewPsw();
			}else  if(psw_action==INPUT_PSW){
				inputUserPsw();
			}
			break;
		}
		
	}
	
	
	private void  ckeckFactoryPsw(){
		if(inputPsw.equals(FACTORY_PSW)){
			psw_action = CHANGE_PSW;
			mPswImg.setImageResource(R.drawable.pwd_icon0);
			titleTx.setText(R.string.input_new_psw_title);
			//Intent intent = new Intent(MainActivity.this,MainFragment.class);
			//startActivity(intent);
		}else{
			Toast.makeText(MainActivity.this, "密码错误，请重新输入", 2000).show();
			mPswImg.setImageResource(R.drawable.pwd_icon0);
		}
		inputPsw ="";
	}
	
	
	private void inputUserNewPsw(){
		if(userPsw==null){
			userPsw =inputPsw;
			mPswImg.setImageResource(R.drawable.pwd_icon0);
			titleTx.setText(R.string.input_new_psw_title2);
		}else if(inputPsw.equals(userPsw)){
			dm.storeUserPsw(inputPsw);
			Intent intent = new Intent(MainActivity.this,MainFragment.class);
			startActivity(intent);	
		}else{
			Toast.makeText(MainActivity.this, "两次输入的密码不一致，请重新输入", 2000).show();
			mPswImg.setImageResource(R.drawable.pwd_icon0);
		}
		inputPsw ="";
	}
	
	
	private void inputUserPsw(){
       if(inputPsw.equals(userPsw)){
			dm.storeUserPsw(inputPsw);
			Intent intent = new Intent(MainActivity.this,MainFragment.class);
			startActivity(intent);
            finish();//close the psw activity
		}else{
			Toast.makeText(MainActivity.this, "密码不正确，请重新输入", 2000).show();
			mPswImg.setImageResource(R.drawable.pwd_icon0);
		}
		inputPsw ="";
	}

}
